package com.ycy.chuwenuniapp.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.chuwenuniapp.common.Constant;
import com.ycy.chuwenuniapp.common.ResCodeEnum;
import com.ycy.chuwenuniapp.common.ResData;
import com.ycy.chuwenuniapp.entity.GoodsColor;
import com.ycy.chuwenuniapp.entity.User;
import com.ycy.chuwenuniapp.exception.CustomException;
import com.ycy.chuwenuniapp.mapper.IGoodsColorMapper;
import com.ycy.chuwenuniapp.mapper.IUserMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService extends ServiceImpl<IUserMapper, User> {
    //增加或修改
    public ResData<Boolean> saveUser(User entity) {
        String openId = entity.getOpenId();
        String name = entity.getName();
        if(StrUtil.isBlank(openId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失openId");
        if(StrUtil.isBlank(name)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失用户名");

        return ResData.success(saveOrUpdate(entity));
    }

    public ResData<User> findOne(String userId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        List<User> list = list(wrapper);
        if(list.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该id的用户信息不存在");
        if(list.size() > 1) throw new CustomException(ResCodeEnum.SERVER_ERROR,"错误的存在多个该id的账号");
        return ResData.success(list.get(0));
    }

    //设置用户为管理员
    public Boolean updateAdminFlag(String userId) {
        if(StrUtil.isBlank(userId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失用户id");
        User user = new User();
        user.setId(userId);
        user.setIsAdmin(1);
        return updateById(user);
    }

    //查全部
    public List<User> findAll(String openId, String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(openId),"open_id",openId);
        wrapper.like(StrUtil.isNotBlank(name),"name",name);
        return list(wrapper);
    }
//
//    public Boolean delete(List<String> sizeIdList) {
//        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
//        wrapper.in(sizeIdList.size() != 0,"size_id",sizeIdList);
//        return remove(wrapper);
//    }
}
